package com.sweets.look.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

import com.sweets.look.constant.Constants;
import com.sweets.look.preference.PreferenceManager;
import com.sweets.look.ui.activity.BrowserActivity;

public final class Utils {
    private final static String TAG = Utils.class.getSimpleName();

    public static boolean checkAddon(Context context, String packageName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException v0) {
            packageInfo = null;
        }
        if (packageInfo != null) {
            return true;
        }
        return false;
    }

    public static boolean checkPermission(Context context, String permission) {
        if (Constants.API < 23)
            return true;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {//0
                return true;
            }
        }
        return false;
    }

    public static void getAddons(Activity activity, String packageName) {
        String v0 = "http://fir.im/" + packageName.replaceAll("\\.", "");
        LogUtils.w(TAG, "install add-ons : " + v0);
        activity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(v0), ((Context) activity),
                BrowserActivity.class));
    }

    public static String getAppPackageName(Context context) {
        String packageName = null;
        try {
            packageName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).packageName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return packageName;
    }

    public static int getAppVersionCode(Context context) {
        int versionCode = 0;
        try {
            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return versionCode;
    }

    public static String getAppVersionName(Context context) {
        String v1 = null;
        try {
            v1 = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException v0) {
            v0.printStackTrace();
        }

        return v1;
    }

    public static String getDownloadPath() {
        return Constants.EXTERNAL_STORAGE + "/" + PreferenceManager.getInstance().getDownloadDirectory()
                + "/";
    }

    public static boolean isPiracy(Context context) {
        try {
            int v0 = context.getPackageManager().getPackageInfo(context.getPackageName(),
                    PackageManager.GET_SIGNATURES).signatures[
                    0].hashCode();//64
            LogUtils.e(TAG, "check key: " + String.valueOf(v0));
            if (v0 == 290049049) {
                return false;
            }
        } catch (PackageManager.NameNotFoundException v5) {
            return false;
        }

        return true;
    }

    public static void requestPermission(Context context, String[] permission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < permission.length; i++) {
                if (context.checkSelfPermission(permission[i]) != PackageManager.PERMISSION_GRANTED) {
                    ((Activity) context).requestPermissions(new String[]{permission[i]}, 111);
                }

            }
        }
    }

    public static void updateApp(Activity activity) {

        Intent v1 = new Intent("android.intent.action.VIEW");
        v1.setComponent(null);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            v1.setSelector(null);
        }


        v1.setData(Uri.parse("market://details?id=" + activity.getPackageName()));
        try {
            activity.startActivity(v1);
        } catch (ActivityNotFoundException v0) {
            activity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse("http://fir.im/"), ((
                    Context) activity), BrowserActivity.class));
        }
    }
}

